
package mwn.droidbus.directions;

import com.google.android.maps.GeoPoint;
import java.util.ArrayList;  
import java.util.Scanner;  
import java.io.File;
import java.util.StringTokenizer;

//parses kml file for geopoints to place in array
//currently called by mapscraper

public class KmlParser { //data from file into nav set

	//int*1000000) .intValue
	//<LineString><coordinates>138.557400,-34.928760,0.000000</coordinates
	public static ArrayList<GeoPoint> parse( String filename ){
		
		String line;
		ArrayList<GeoPoint> result = new ArrayList<GeoPoint>();
		
		try {
			
			Scanner scanner = new Scanner(new File(filename));
		    scanner.useDelimiter("LineString");		    	//linestring is always and only present just before the full collection of coordinates	  
		    scanner.next();									//skip all the stuff beforehand
		    line = scanner.next(); //coords
		    StringTokenizer stPoint = new StringTokenizer( line , " >" );  //prepare to discare the "coordinates" uml tags
		    
		    while ( stPoint.hasMoreTokens() ){
				
				String pointToken = stPoint.nextToken();
			
				if ( !pointToken.contains("<")  ){ //discard non point tokens - get to the meat
				
					StringTokenizer stVal = new StringTokenizer( pointToken , "," );  //parse coord values
					Double lat = Double.parseDouble( stVal.nextToken() ) * 1000000;  //convert to geopoint while we're at it
					Double lon = Double.parseDouble( stVal.nextToken() ) * 1000000;
					result.add( new GeoPoint( lat.intValue() , lon.intValue() )  ); 
					//System.out.println( "new GeoPoint( "+lat.intValue()+" , "+lon.intValue()+" )  ");
				
				}
		    
		    	
			}
			
		    scanner.close();
		    
		       
		}catch (Exception e) {
		    	//die
		}
		   
		
		
		
		return null;
	
	}//parse
	
	
}
